package com.ruoyi.web.controller.activities;

import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.ActivityPictureMapping;
import com.ruoyi.system.service.IActivityPictureMappingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 配图管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-05
 */

@RestController
@RequestMapping("/activities/mapping")
@Api("动态配图页面接口")
//@ComponentScan({"com.ruoyyi.*","com.test.*"})
public class ActivityPictureMappingController extends BaseController
{
    @Autowired
    private IActivityPictureMappingService activityPictureMappingService;

//    // 上传路径配置
//    private final String uploadPath = "D:/ruoyi/uploadPath";

//    // 上传接口
//    @PostMapping("/upload")
//    public ResponseEntity<Map<String, String>> uploadImage(@RequestParam("file") MultipartFile file) {
//        Map<String, String> response = new HashMap<>();
//        try {
//            // 检查文件类型
//            if (!file.getContentType().startsWith("image/")) {
//                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Collections.singletonMap("error", "文件必须是图片"));
//            }
//
//            // 构造保存路径，使用 UUID 防止文件名重复
//            String fileName = UUID.randomUUID().toString() + "." + file.getOriginalFilename().split("\\.")[1];
//            File dest = new File(uploadPath + "/" + fileName);
//
//            // 创建文件夹（如果不存在的话）
//            dest.getParentFile().mkdirs();
//
//            // 保存文件
//            file.transferTo(dest);
//
//            // 返回文件 URL
//            response.put("url", "/uploads/" + fileName);  // 这里返回的 URL 用于前端访问
//            return ResponseEntity.ok(response);
//        } catch (IOException e) {
//            response.put("error", "文件上传失败");
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
//        }
//    }

    @ApiOperation("测试接口")
    @GetMapping("/test")
    public String test()
    {
        return "testOK";
    }

    /**
     * 查询配图管理列表
     */
    @Anonymous
    @ApiOperation("配图查询接口")
    @GetMapping("/list")
    public TableDataInfo list(ActivityPictureMapping activityPictureMapping)
    {
        startPage();
        List<ActivityPictureMapping> list = activityPictureMappingService.selectActivityPictureMappingList(activityPictureMapping);
        return getDataTable(list);
    }

    /**
     * 导出配图管理列表
     */

    @Log(title = "配图管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActivityPictureMapping activityPictureMapping)
    {
        List<ActivityPictureMapping> list = activityPictureMappingService.selectActivityPictureMappingList(activityPictureMapping);
        ExcelUtil<ActivityPictureMapping> util = new ExcelUtil<ActivityPictureMapping>(ActivityPictureMapping.class);
        util.exportExcel(response, list, "配图管理数据");
    }

    /**
     * 获取配图管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping(value = "/{mappingId}")
    public AjaxResult getInfo(@PathVariable("mappingId") Long mappingId)
    {
        return success(activityPictureMappingService.selectActivityPictureMappingByMappingId(mappingId));
    }

    /**
     * 新增配图管理
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "配图管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityPictureMapping activityPictureMapping)
    {
        return toAjax(activityPictureMappingService.insertActivityPictureMapping(activityPictureMapping));
    }
    /**
     * 修改配图管理
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "配图管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityPictureMapping activityPictureMapping)
    {
        return toAjax(activityPictureMappingService.updateActivityPictureMapping(activityPictureMapping));
    }

    /**
     * 删除配图管理
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "配图管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mappingIds}")
    public AjaxResult remove(@PathVariable Long[] mappingIds)
    {
        return toAjax(activityPictureMappingService.deleteActivityPictureMappingByMappingIds(mappingIds));
    }
}
